﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;


namespace TAMS
{
    public partial class login : System.Web.UI.Page
    {
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        MySqlConnection conn;

        /*
         * redirects page according to the, cookie's stored userType data
         */
        private void redirectAccordingToUserType(string userType)
        {
            if (userType.Equals("Admin"))
            {
                Response.Redirect("Admin/Admin_Welcome.aspx");
            }

            else if (userType.Equals("TA"))
            {
                Response.Redirect("TA/TA_Welcome.aspx");
            }

            else if (userType.Equals("RA"))
            {
                Response.Redirect("RA/RA_Welcome.aspx");
            }

            else if (userType.Equals("Faculty Member"))
            {
                Response.Redirect("Faculty Member/FacultyMember_Welcome.aspx");
            }

            else if (userType.Equals("Instructor"))
            {
                Response.Redirect("Instructor/Instructor_Welcome.aspx");
                //asd
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies.Get("LastVisit") != null)
            {
                string uType = Request.Cookies["LastVisit"]["userType"];
                redirectAccordingToUserType(uType);
            }
            
            conn = new MySqlConnection(Resource1.databaseConnection);
            conn.Open();
        }

        /*
         * if remember me checked, creates a cookie, to remember client
         * else it clears previous cookie from cookies.
         */
        private void manageCookies(String userName, String pass)
        {
            if (RememberMe.Checked)
            {
                Response.Cookies["LastVisit"]["userName"] = userName;
                Response.Cookies["LastVisit"]["password"] = pass;
                Response.Cookies["LastVisit"]["userType"] = DropDownList1.SelectedItem.Text;
                Response.Cookies["LastVisit"].Expires = DateTime.Now.AddDays(1);
            }

            else
            {
                HttpCookie tmp = new HttpCookie("LastVisit");
                tmp.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(tmp);
            }
        }


        protected void LoginButton_Click(object sender, EventArgs e)
        {
            String selection = DropDownList1.SelectedItem.Text;
            String userID,password;

            userID = UserName.Text;
            password = Password.Text;

            

            if (selection.Equals("Admin"))
            {
                string adminQuery = "SELECT *"+
                "FROM Admin A " +
                "WHERE A.adminId = '"
                + userID + "' AND A.a_pass= '" + password+"'";

                sqlCommand = new MySqlCommand(adminQuery, conn);
                readVals = sqlCommand.ExecuteReader();

                if (readVals.HasRows)
                {
                    manageCookies(userID, password);

                    // Adding his ID in session
                    Session["userName"] = userID;

                    /*  Admin Welcome
                     */
                    Response.Redirect("Admin/Admin_Welcome.aspx");
                }
                else
                    FailureText.Text = "Wrong user information, check your id and password !";

            }

            else if (selection.Equals("TA"))
            {
                string adminQuery = "SELECT *" +
               "FROM TA T, GradStudent G " +
               "WHERE T.ta_sid = '"
               + userID + "' AND G.s_pass= '" + password + "' AND G.sid = T.ta_sid";

                sqlCommand = new MySqlCommand(adminQuery, conn);
                readVals = sqlCommand.ExecuteReader();

                if (readVals.HasRows)
                {
                    manageCookies(userID, password);

                    // Adding his ID in session
                    Session["userName"] = userID;

                    /* TA Welcome
                     */
                    Server.Transfer("TA/TA_Welcome.aspx");
                }
                else
                    FailureText.Text = "Wrong user information, check your id and password !";
            }

            else if (selection.Equals("RA"))
            {
                string adminQuery = "SELECT *" +
               "FROM RA R, GradStudent G " +
               "WHERE R.ra_sid = '"
               + userID + "' AND G.s_pass= '" + password + "' AND G.sid = R.ra_sid";

                sqlCommand = new MySqlCommand(adminQuery, conn);
                readVals = sqlCommand.ExecuteReader();

                if (readVals.HasRows)
                {
                    manageCookies(userID, password);

                    // Adding his ID in session
                    Session["userName"] = userID;

                    /* RA Welcome Page
                     */
                    Server.Transfer("RA/RA_Welcome.aspx");
                }
                else
                    FailureText.Text = "Wrong user information, check your id and password !";
            }
            else if (selection.Equals("Faculty Member"))
            {
                string adminQuery = "SELECT *" +
               "FROM FMember F, Staff S " +
               "WHERE F.fmem_fsid = '"
               + userID + "' AND S.fs_pass= '" + password + "' AND S.fsid = F.fmem_fsid";

                sqlCommand = new MySqlCommand(adminQuery, conn);
                readVals = sqlCommand.ExecuteReader();

                if (readVals.HasRows)
                {
                    manageCookies(userID, password);
                    // Adding his ID in session
                    Session["userName"] = userID;

                    /* Facult Member Welcome Page
                     */
                    Server.Transfer("Faculty Member/FacultyMember_Welcome.aspx");
                }
                else
                    FailureText.Text = "Wrong user information, check your id and password !";
            }
            else if (selection.Equals("Instructor"))
            {
                string adminQuery = "SELECT *" +
                "FROM Instructor I, Staff S " +
                "WHERE I.ins_fsid = '"
                + userID + "' AND S.fs_pass= '" + password + "' AND S.fsid = I.ins_fsid";

                sqlCommand = new MySqlCommand(adminQuery, conn);
                readVals = sqlCommand.ExecuteReader();

                if (readVals.HasRows)
                {
                    manageCookies(userID, password);
                    // Adding his ID in session
                    Session["userName"] = userID;

                    /* Instructor Welcome Page
                     */
                    Server.Transfer("Instructor/Instructor_Welcome.aspx");
                }
                else
                    FailureText.Text = "Wrong user information, check your id and password !";
            }
        }

        protected void RememberMe_CheckedChanged(object sender, EventArgs e)
        {
           
        }

        protected void UserName_TextChanged(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


    }
}